package com.ztools.views;

import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

/**
 * Created by zhangxuehui on 2018/5/11.
 */

public class TextViewHelper {

    public static void setTextColorForTargetText(@NonNull TextView textView, @NonNull String targetStr, @ColorInt int resId) {
        String str = textView.getText().toString();
        SpannableStringBuilder style = new SpannableStringBuilder(str);
        style.setSpan(new ForegroundColorSpan(resId), str.indexOf(targetStr), str.indexOf(targetStr) + targetStr.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        textView.setText(style);
    }

    public static void setTextSizeForTargetText(@NonNull TextView textView, @NonNull String targetStr, int deminId) {
        String str = textView.getText().toString();
        SpannableStringBuilder style = new SpannableStringBuilder(str);
        style.setSpan(new AbsoluteSizeSpan(deminId, true), str.indexOf(targetStr), str.indexOf(targetStr) + targetStr.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        textView.setText(style);
    }

    public static void setTextSizeWithColorForTargetText(@NonNull TextView textView, @NonNull String targetStr, @ColorInt int resId, int deminId) {
        String str = textView.getText().toString();
        SpannableStringBuilder style = new SpannableStringBuilder(str);
        style.setSpan(new AbsoluteSizeSpan(deminId, true), str.indexOf(targetStr), str.indexOf(targetStr) + targetStr.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        style.setSpan(new ForegroundColorSpan(resId), str.indexOf(targetStr), str.indexOf(targetStr) + targetStr.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        textView.setText(style);
    }

}
